package com.study.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.book.common.R;
import com.study.book.pojo.Admin;
import com.study.book.res.req.LoginReqVo;
import com.study.book.service.AdminService;
import com.study.book.mapper.AdminMapper;
import com.study.book.utils.JwtUtils;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.Random;

/**
* @author ASUS
* @description 针对表【Admin】的数据库操作Service实现
* @createDate 2024-05-05 18:18:07
*/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
    implements AdminService{

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private JwtUtils jwtUtils;

    @Override
    public R appLogin(LoginReqVo vo) {
        if (Strings.isEmpty(vo.getUsername()) || Strings.isEmpty(vo.getPassword())){
            return R.error("用户名或密码不能为空");
        }
        LambdaQueryWrapper<Admin> adminLambdaQueryWrapper = new LambdaQueryWrapper<>();
        adminLambdaQueryWrapper.eq(Admin::getPhone, vo.getUsername()).eq(Admin::getType,"0");

        Admin admin = adminMapper.selectOne(adminLambdaQueryWrapper);
        if (admin == null){
            Admin adminUser = new Admin().setUsername("默认用户"+ (new Random().nextInt(899999) + 100000)).setPhone(vo.getUsername()).setPassword(vo.getPassword()).setType("0");
            int insert = adminMapper.insert(adminUser);
            if (insert > 0){
              return R.ok( "success",jwtUtils.generateToken(adminUser.getId().toString()));
            }
            return R.error("用户注册失败");
        }else{
            if (admin.getPassword().equals(vo.getPassword())){
                return R.ok( "success",jwtUtils.generateToken(admin.getId().toString()));
            }else{
                return R.error("用户名或密码错误");
            }
        }
    }

    @Override
    public R<Admin> userInfo(String id) {
        Admin admin = adminMapper.selectById(id);
        return R.ok(admin);
    }

    @Override
    @Transactional
    public R updateUserInfo(Admin admin) {
        if (Objects.isNull(admin.getId())){
            return R.error("用户id不能为空");
        }
        int update = adminMapper.updateById(admin);
        if (update > 0){
            return R.ok(admin);
        }
        return R.error("更新失败");
    }

    @Override
    public long uploadImages(MultipartFile file, String suffix) throws IOException {

        String savePath = "data/images/avatar/";
        long fileId = System.currentTimeMillis();
        String filename = fileId + "." + suffix;
        FileUtils.copyInputStreamToFile(file.getInputStream(), new File(System.getProperty("user.dir") + "/src/main/resources/" + savePath + filename));
        return fileId;
    }

    @Override
    public Page<Admin> list(LambdaQueryWrapper lambdaQueryWrapper,Page page) {
        return adminMapper.selectPage(page, lambdaQueryWrapper);
    }
}




